package com.baidu.springcloud.controller;

import com.baidu.springcloud.bean.Payment;
import com.baidu.springcloud.common.CommonResult;
import com.baidu.springcloud.resultenum.ResultEnum;
import com.baidu.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author ShiLei
 * @version v1.0
 * @date 2020-03-11 10:57
 */
@RestController
@RequestMapping("api/payment")
@CrossOrigin
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("save")
    public CommonResult save(@RequestBody Payment payment) {
        int i = paymentService.savePayment(payment);
        if (i > 0) {
            return new CommonResult<>(ResultEnum.SUCCESS_RESULT.getCode(), ResultEnum.SUCCESS_RESULT.getMessage());
        }
        return new CommonResult<>(ResultEnum.ERROR_RESULT.getCode(), ResultEnum.ERROR_RESULT.getMessage(),serverPort);
    }

    @GetMapping("getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        return new CommonResult<>(ResultEnum.SUCCESS_RESULT.getCode(), ResultEnum.SUCCESS_RESULT.getMessage() + serverPort, paymentById);
    }
    @GetMapping("lb")
    public String getPaymentLb(){
        return serverPort;
    }
}
